// 导入MySql模块
var mysql = require('mysql');
var dbConfig = require('../db/DBConfig');
var userSQL = require('../db/Usersql');
var express = require('express');
var router = express.Router();


// 使用DBConfig.js的配置信息创建一个MySql链接池
var pool = mysql.createPool( dbConfig.mysql );
// 响应一个JSON数据
var responseJSON = function (res, ret) {
    if (typeof ret === 'undefined') {
      res.json({
          code: '-200',
          msg: '操作失败'
      });
    } else {
      res.json(ret);
    }
};



// 用户登录
router.get('/login',function (req, res, next) {
    // 从连接池获取连接
    pool.getConnection(function (err, connection) {
        // 获取前台页面传过来的参数
        var param = req.query || req.params;
        var user_id;
        var UserName = param.user_name;
        var Password = param.user_password;
        var user_longitude = param.user_longitude;
        var user_latitude = param.user_latitude;
        //var login_time = param.login_time;
        var _res = res;
        connection.query(userSQL.queryAll, function (err, res, result) {
            var isTrue = false;
            if(res){ //获取用户列表，循环遍历判断当前用户是否存在
                for (var i=0;i<res.length;i++) {
                    if(res[i].user_name == UserName && res[i].user_password == Password) {
                        isTrue = true;
                    }
                }
            }
            var data = {};
            data.isLogin = isTrue; //如果isLogin布尔值为true则登录成功 有false则失败
            if(isTrue) {
                data.userInfo = {};
                data.userInfo.user_name = UserName;
                data.userInfo.user_pasword = Password;
                data.result = {
                    code: 200,
                    msg: '登录成功'
                };//登录成功返回用户信息
            } //登录成功返回用户信息
            else{
                data.result = {
                    code: -1,
                    msg: '登录失败'
                };//登录成功返回用户信息
            }

            if(err) data.err = err;
            // 以json形式，把操作结果返回给前台页面
            responseJSON(_res, data);

            // 释放链接
            connection.release();

        });
    });
});
//将登录信息存入login表
router.get('/logMessage',function (req, res, next) {
    var param = req.query || req.params;

    function getUserId(username,callback)
    {
        connection.query (userSQL.getUsers,[username],function(err,result)
        {

            if(err)
                callback(err,null);
            else
                callback(null,result [0] .user_id);

        });

    }

    var user_id = getUserId(param.user_name,fuction(err,data));
    // 从连接池获取连接
    pool.getConnection(function (err, connection) {

        connection.query(userSQL.login_insert,[user_id,param.user_longitude,param.user_latitude,param.login_time], function (err, result){
            if(result) {
                result = {
                    code: 200,
                    msg: '存入login表成功'
                };
            }
            responseJSON(res, result);
            connection.release();
        });

    });
});
// 用户注册
router.get('/reg',function (req, res, next) {
    // 从连接池获取连接
    pool.getConnection(function (err, connection) {
        // 获取前台页面传过来的参数
        var param = req.query || req.params;
        var UserName = param.user_name;
        var Password = param.user_password;
        var Mobile = param.user_mobile;
        var _res = res;
        connection.query(userSQL.queryAll, function (err, res) {
            var isTrue = false;
            if(res){ //获取用户列表，循环遍历判断当前用户是否存在
                for (var i=0;i<res.length;i++) {
                    if(res[i].user_name == UserName) {
                        isTrue = true;
                    }
                }
            }
            var data = {};
            data.isreg = !isTrue; //如果isreg布尔值为true则注册成功 有false则失败
            if(isTrue) {
                data.result = {
                    code: 1,
                    msg: '用户已存在'
                };//登录成功返回用户信息
            } else {
                connection.query(userSQL.insert, [param.user_name,param.user_password,param.user_mobile], function (err, result) {
                    if(result) {
                        data.result = {
                            code: 200,
                            msg: '注册成功'
                        };
                    } else {
                        data.result = {
                            code: -1,
                            msg: '注册失败'
                        };
                    }
                });
            }

            if(err) data.err = err;
            // 以json形式，把操作结果返回给前台页面
            setTimeout(function () {
                responseJSON(_res, data)
            },300);
            // responseJSON(_res, data);
            // 释放链接
            connection.release();

        });
    });
});
// 删除用户
// router.get('/delUser',function (req, res, next) {
//     var _res = res;
//     pool.getConnection(function (err, connection) {
//         var param = req.query || req.params;
//         connection.query(userSQL.delete, [param.uid], function (err, result, res) {
//             if(result) {
//                 result = {
//                     code: 200,
//                     msg: '刪除成功'
//                 };
//             }
//             responseJSON(_res, result);
//             connection.release();
//         })
//     })
// });



// 删除user表
 router.get('/dropUser',function (req, res, next) {
     pool.getConnection(function (err, connection) {
         connection.query(userSQL.dropUser, function (err, result){
             if(result) {
                 result = {
                     code: 200,
                     msg: '清除user表成功'
                 };
             }
             responseJSON(res, result);
             connection.release();
         });
     });
 });
 //初始化user表
router.get('/initUser',function (req, res, next) {
    pool.getConnection(function (err, connection) {
        connection.query(userSQL.initUser, function (err, result){
            if(result) {
                result = {
                    code: 200,
                    msg: '创建user表成功'
                };
            }
            responseJSON(res, result);
            connection.release();
        });
    });
});

// 删除Login表
router.get('/dropLogin',function (req, res, next) {
    pool.getConnection(function (err, connection) {
        connection.query(userSQL.dropLogin, function (err, result){
            if(result) {
                result = {
                    code: 200,
                    msg: '清除login表成功'
                };
            }
            responseJSON(res, result);
            connection.release();
        });
    });
});
//初始化login表
router.get('/initLogin',function (req, res, next) {
    pool.getConnection(function (err, connection) {
        connection.query(userSQL.initLogin, function (err, result){
            if(result) {
                result = {
                    code: 200,
                    msg: '创建login表成功'
                };
            }
            responseJSON(res, result);
            connection.release();
        });
    });
});

// 删除用户表
// router.get('/dropUser',function (req, res, next) {
//     pool.getConnection(function (err, connection) {
//         // var param = req.query || req.params;
//         connection.query(userSQL.drop, function (err, result) {
//             if(result) {
//                 result = {
//                     code: 200,
//                     msg: '清除成功'
//                 };
//             }
//             responseJSON(res, result);
//             connection.release();
//         });
//     });
// });
 //查询用户表
 router.get('/queryAllUser',function (req, res, next) {
     var _res = res;
     pool.getConnection(function (err, connection) {
         connection.query(userSQL.queryAll, function (err, result, res) {
             console.log(res);
             if(result) {
                 result = {
                     code: 200,
                     msg: '查询成功'
                 };
             }
             responseJSON(_res, result);
             connection.release();
         });
     });
 });
// 查询用户
 router.get('/getUserByIdUser',function (req, res, next) {
     pool.getConnection(function (err, connection) {
         var param = req.query || req.params;
         connection.query(userSQL.getUserById, [param.user_id], function (err, result) {
             if(result) {
                 result = {
                     code: 200,
                     msg: '查询成功'
                 };
             }
            responseJSON(res, result);
             connection.release();
         });
     });
 });
// 模糊查询
 /*router.get('/getUsers',function (req, res, next) {
     pool.getConnection(function (err, connection) {
         var param = req.query || req.params;
        connection.query(userSQL.getUsers, [param.uid], function (err, result) {
             if(result) {
                 result = {
                    code: 200,
                    msg: '查询成功'
                 };
             }
             responseJSON(res, result);
             connection.release();
         });
     });
 });*/
/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('in users page');
});

module.exports = router;
